/**
 * Created by root on 15-1-2.
 */

define(['lessonJS/module'], function(lessonModule){

  lessonModule.directive('lessonInfo', ['lessonModuleBaseUrl', function(lessonModuleBaseUrl){
    return {
      restrict: 'EA',
      templateUrl: lessonModuleBaseUrl + 'tpls/lesson-info.html',
      controller: 'LessonInfoController',
      scope: {
      }
    }
  }]);

  lessonModule.directive('callnameBox', ['$rootScope', '$timeout', '$state', '$interval', 'lessonModuleBaseUrl', 'ClassHelperService',
    function($rootScope, $timeout, $state, $interval, baseUrl, ClassHelperService){

      var intervalCallbackFun = null;
      var studentsCanSelected = null;

      function removeSpecialStudent(students, specialStudent){
        var targetIndex = null;
        for( var index = 0, length = students.length; index < length; ++index){
          if( ClassHelperService.objectEquals(students[index], specialStudent) ){
            targetIndex = index;
            break;
          }
        }
        students.splice(targetIndex, 1);
      }

      function doing(scope, intervalTime){
        intervalTime = intervalTime || 90;

        var total = studentsCanSelected.length;

        intervalCallbackFun = $interval(function(){
          var nextIndex = Math.floor(Math.random() * total );
          scope.currentStudent = studentsCanSelected[ nextIndex ];

        }, intervalTime);
      }



      function reset(scope){
        if( intervalCallbackFun !== null ){
          $interval.cancel(intervalCallbackFun);
          intervalCallbackFun = null;
        }

        studentsCanSelected = scope.students.slice(0);
        scope.showConfirm = false;
        scope.currentStudent = null;
        scope.markedStudents = [];
      }

      var modeStatusHandler = {
        'auto': {
          start: function(scope){
            doing(scope);

            $timeout(function(){
              $interval.cancel(intervalCallbackFun);
              scope.showConfirm = true;
            }, 2000);
          },
          pause: function(){
            $interval.cancel(intervalCallbackFun);
            this._pause = true;
          },
          again: function(scope){
            if( ! this._pause ){
              this.start(scope);
            }
          },
          reset: reset
        },
        'manual': {
          start: doing,
          pause: function(scope){
            $interval.cancel(intervalCallbackFun);
            scope.showConfirm = true;
          },
          reset: reset,
          again: doing
        }
      };

      return {
        restrict: 'EA',
        templateUrl: baseUrl + 'tpls/callname-box.html',
        scope: {
          status: '=',
          students: '=',
          markedStudents: '=',
          mode: '=',
          handler: '='
        },
        link: function(scope){

          scope.handler = {
            start: function () {
              scope.status = 'doing';
              modeStatusHandler[scope.mode].start(scope);
            },
            pause: function () {
              modeStatusHandler[scope.mode].pause(scope);
            },
            reset: function(){
              scope.status = 'stop';
              modeStatusHandler[scope.mode].reset(scope);
            },
            setMode: function(mode){
              scope.mode = mode;
            }
          };

          reset(scope);

          scope.markStudent = function(mark){
            scope.currentStudent['mark'] = mark;
            scope.markedStudents.push(scope.currentStudent);

            removeSpecialStudent(studentsCanSelected, scope.currentStudent);

            modeStatusHandler[scope.mode].again(scope);
            scope.showConfirm = false;
          };//End of --> function:markStudent



          var initOnState = $state.current.name;
          //当离开页面时自动暂停
          $rootScope.$on('$stateChangeStart', function(event, toState){
            if( toState.name.indexOf(initOnState) === -1 && studentsCanSelected ){
              reset(scope);
            }
          });//End of --> $rootScope.$on
        }
      };
  }]);
});